package camadaSO;

import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.ExportException;
import java.rmi.server.UnicastRemoteObject;
import java.util.List;

public class ClienteStub extends UnicastRemoteObject implements IServidorClienteRMI{
	
	protected ClienteStub() throws RemoteException {
		super();
	}

	private static camadaSO.IClienteServidorRMI servidor;
	
	public static camadaSO.IClienteServidorRMI getServidor() {
		return servidor;
	}

	private static void setServidor(camadaSO.IClienteServidorRMI servidor) {
		ClienteStub.servidor = servidor;
	}
	
    public static void conectarServidor(){
    	try {
			setServidor((camadaSO.IClienteServidorRMI) java.rmi.Naming.lookup("//localhost//Servidor"));
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (NotBoundException e) {
			e.printStackTrace();
		}
    }
    
	public boolean iniciarClienteRMI() {
        try {        	        	
        	try {
				LocateRegistry.createRegistry(1099);
			} catch (ExportException e) {

			}
        	
        	ClienteStub obj = new ClienteStub();
        	String ipCliente = InetAddress.getLocalHost().getHostAddress();		

            Naming.rebind("//"+ipCliente+"//Cliente", obj);

            return true;
        } catch (MalformedURLException ex) {
			System.out.println( "MalformedURLException: " + ex.toString() );
        } catch (RemoteException ex) {
			System.out.println( "RemoteException: " + ex.toString() ); 
        } catch (UnknownHostException e) {
			e.printStackTrace();
		}
        return false;
    }
	
	public boolean finalizarClienteRMI() {
        try { 	
        	String ipCliente = InetAddress.getLocalHost().getHostAddress();

            Naming.unbind("//"+ipCliente+"//Cliente");

            return true;
        } catch (RemoteException ex) {
			System.out.println( "RemoteException: " + ex.toString() ); 
        } catch (NotBoundException ex) {
			System.out.println( "NotBoundException: " + ex.toString() );
        } catch (MalformedURLException ex) {
			System.out.println( "MalformedURLException: " + ex.toString() );
        } catch (UnknownHostException e) {
			e.printStackTrace();
		}
        return false;
    }

	public void apresentarMensagemDesafio(String desafiante)
			throws RemoteException {
		
	}

	public void apresentarRespostaDesafio(boolean resposta, String desafiado)
			throws RemoteException {
		
	}

	public void atualizaTela(int posicaoY, int posicaoX, int posicaoY2,
			int posicaoX2) throws RemoteException {
		
	}

	public void atualizarPecasJogador(int xpeca1, int ypeca1, int xpeca2,
			int ypeca2, String movimento) throws RemoteException {
		
	}

	public void atualizarPecasJogadorAposCombate(int xpeca1, int ypeca1,
			int xpeca2, int ypeca2, String vencedor, String resultado)
			throws RemoteException {
		
	}

	public void finalizaPartida(boolean ganhou) throws RemoteException {
		
	}

	public void habilitaTabuleiro(String adversario) throws RemoteException {
		
	}

	public void limpaListaAtual() throws RemoteException {
		
	}

	public void passaListaPCliente(List<String> nomes, List<String> senhas,
			List<Integer> pontuacoes) throws RemoteException {
		
	}


}
